package com.mkyong.util;
 
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
 
@SuppressWarnings("deprecation")
public class HibernateUtil {
	/* 
	 * Hibernate se inicia construyendo un objeto global "org.hibernate.SessionFactory" 
	 * que permite obtener instancias de "org.hibernate.Session".
	 * Un org.hibernate.Session representa una unidad de un solo subproceso de trabajo.
	*/
	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			return new AnnotationConfiguration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
 
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
 
	public static void shutdown() {
		// Close caches and connection pools
		getSessionFactory().close();
	}
 
}